__type__ = 'seen'
__instance__ = 'SeenFilter'

import urllib
import logging

class SeenFilter:

    """
        Filter out torrents that have already been seen before.
        This module is enabled on all sources even when no filters are configured. This
        can be disabled by setting source level setting disable_seen_filter value to True
    """

    def run(self, src,  matches, config, session):
        if not session.has_key('entries'):
            session['entries'] = []
            
        for match in matches:
            torrent_url = urllib.unquote(match['torrent']).encode()
            if torrent_url in session['entries'] or match['title'] in session['entries']:
                logging.debug("SeenFilter: filtering '%s' '%s'" % (torrent_url, match['title']))
                match['filtered'] = True
            else:
                # add title and torrent url to seen list
                session['entries'].append(match['title'].encode()) # encode since yml load fails with unicode
                session['entries'].append(torrent_url)
                logging.debug("SeenFilter: learned '%s' '%s' (skipped in future)" % (torrent_url, match['title']))
                # if entries has grown too big, remove some of the old ones
                while len(session['entries']) > 5000:
                    session['entries'].pop(0)
